package com.jenings.shop.util;

import com.jenings.shop.file.FastDFSFile;
import org.csource.common.MyException;
import org.csource.common.NameValuePair;
import org.csource.fastdfs.*;
import org.springframework.core.io.ClassPathResource;

import java.io.IOException;


public class FastDFSClient {


    // 加载FastDFS的配置文件
    static {
        String path = "fdfs_client.conf";
        ClassPathResource resource = new ClassPathResource(path);
        String conf_filename = resource.getPath();
        // 初始化FastDFS配置文件
        try {
            ClientGlobal.init(conf_filename);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (MyException e) {
            e.printStackTrace();
        }
    }

    /**
     * @author 栗子
     * @Description 附件上传
     * @Date 11:18 2019/11/11
     * @param file 附件信息
     * @return java.lang.String[] 大小2，组名+文件存储的磁盘路径
     **/
    public static String[] uploadFile(FastDFSFile file){

        try {
            // 获取附件信息
            byte[] file_buff = file.getContent();
            String file_ext_name = file.getExt();
            NameValuePair[] meta_list = new NameValuePair[1];
            meta_list[0] = new NameValuePair(file.getMd5());

            // 1、创建跟踪服务器的客户端
            TrackerClient trackerClient = new TrackerClient();
            // 2、通过跟踪服务器的客户端获取服务器端
            TrackerServer trackerServer = trackerClient.getConnection();
            // 3、创建存储服务器的客户端
            StorageClient storageClient = new StorageClient(trackerServer, null);
            // 4、将附件进行上传 arg0：附件内容 arg1：附件扩展名称  arg2：附件的备注（null）
            String[] uploadResult = storageClient.upload_file(file_buff, file_ext_name, meta_list);
            return uploadResult;
        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }

    /**
     * @author 栗子
     * @Description 附件下载
     * @Date 12:04 2019/11/11
     * @param group_name        组名
     * @param remote_filename   磁盘的路径
     * @return byte[]
     **/
    public static byte[] downloadFile(String group_name, String remote_filename){

        try {
            // 1、创建跟踪服务器的客户端
            TrackerClient trackerClient = new TrackerClient();
            // 2、通过跟踪服务器的客户端获取服务器端
            TrackerServer trackerServer = trackerClient.getConnection();
            // 3、创建存储服务器的客户端
            StorageClient storageClient = new StorageClient(trackerServer, null);
            // 4、附件下载
            byte[] content = storageClient.download_file(group_name, remote_filename);
            return content;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * @author 栗子
     * @Description 附件删除
     * @Date 12:11 2019/11/11
     * @param group_name        组名
     * @param remote_filename   磁盘的路径
     * @return void
     **/
    public static void deleteFile(String group_name, String remote_filename){
        try {
            // 1、创建跟踪服务器的客户端
            TrackerClient trackerClient = new TrackerClient();
            // 2、通过跟踪服务器的客户端获取服务器端
            TrackerServer trackerServer = trackerClient.getConnection();
            // 3、创建存储服务器的客户端
            StorageClient storageClient = new StorageClient(trackerServer, null);
            // 4、附件删除
            storageClient.delete_file(group_name, remote_filename);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * @author 栗子
     * @Description 获取附件信息
     * @Date 12:15 2019/11/11
     * @param group_name
     * @param remote_filename
     * @return org.csource.fastdfs.FileInfo
     **/
    public static FileInfo getFileInfo(String group_name, String remote_filename){
        try {
            // 1、创建跟踪服务器的客户端
            TrackerClient trackerClient = new TrackerClient();
            // 2、通过跟踪服务器的客户端获取服务器端
            TrackerServer trackerServer = trackerClient.getConnection();
            // 3、创建存储服务器的客户端
            StorageClient storageClient = new StorageClient(trackerServer, null);
            // 4、附件删除
            FileInfo fileInfo = storageClient.get_file_info(group_name, remote_filename);
            return fileInfo;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * @author 栗子
     * @Description 获取存储服务器信息
     * @Date 12:21 2019/11/11
     * @param groupName
     * @return org.csource.fastdfs.StorageServer
     **/
    public static StorageServer getStorageServer(String groupName){
        try {
            // 1、创建跟踪服务器的客户端
            TrackerClient trackerClient = new TrackerClient();
            // 2、通过跟踪服务器的客户端获取服务器端
            TrackerServer trackerServer = trackerClient.getConnection();

            // 获取存储服务器
            StorageServer storeStorage = trackerClient.getStoreStorage(trackerServer, groupName);
            return storeStorage;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * @author 栗子
     * @Description 获取跟踪服务器地址
     * @Date 11:26 2019/11/11
     * @param
     * @return java.lang.String
     **/
    public static String getUrl(){
        try {

            // 1、创建跟踪服务器的客户端
            TrackerClient trackerClient = new TrackerClient();
            // 2、通过跟踪服务器的客户端获取服务器端
            TrackerServer trackerServer = trackerClient.getConnection();
            // 3、获取端口
            int port = ClientGlobal.getG_tracker_http_port();
            // 4、获取ip地址
            String ip = trackerServer.getInetSocketAddress().getAddress().getHostAddress();
            String url = "http://" + ip + ":" + port;
            return url;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

}
